<?php use_helper('jQuery')?>
<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>

<?php echo jq_javascript_tag()?>
    <?php if($form->getObject()->isNew() || $form->getObject()->getSectionId()): ?>
        <?php echo 'initElementMenuFileUpload("'.
                url_for('element_tempfileupload',$form->getObject()->getType()).
                '?section_id='.$form->getObject()->getSectionId().'&portal_id='.
                $form->getObject()->getSection()->getPortalId().'");'
        ?>
    <?php elseif($form->getObject()->getHeaderId()): ?>
        <?php echo 'initElementMenuFileUpload("'.
                url_for('element_tempfileupload',$form->getObject()->getType()).
                '?header_id='.$form->getObject()->getHeaderId().'&portal_id='.
                $form->getObject()->getHeader()->getPortalId().'");'
        ?>
    <?php elseif($form->getObject()->getFooterId()): ?>
        <?php echo 'initElementMenuFileUpload("'.
                url_for('element_tempfileupload',$form->getObject()->getType()).
                '?footer_id='.$form->getObject()->getFooterId().'&portal_id='.
                $form->getObject()->getFooter()->getPortalId().'");'
        ?>
    <?php endif; ?>

    var options =<?php echo $form['Menu']['Options']->count() ?>;

    function addOption(num) {
        var r = $.ajax({
            type: 'POST',
            url: '<?php echo url_for('@element_addmenuoptionform'.(!$form->getObject()->isNew() ?
                            '?id='.$form->getObject()->getId().
                            '&portal_id='.($form->getObject()->getSection()->getPortalId() ?
                                    $form->getObject()->getSection()->getPortalId() :
                                    $form->getObject()->getHeader()->getPortalId()).
                            '&num='
                        :
                            '?type_id='.$form->getObject()->getType()->getId().
                            '&portal_id='.$form->getObject()->getSection()->getPortalId().
                            '&num='))?>'+num,
            async: false}).responseText;
        return r;
    }

    menuBindEventActions();

<?php echo jq_end_javascript_tag(); ?>

<div id="main_header" class="ui-widget-header ui-corner-all">
    <?php if($form->getObject()->exists()):?>
        <h3>Editando Menú: "<?php echo $form->getObject()->getTitle()?>"</h3>
    <?php else:?>
        <h3>Nuevo Menú</h3>
    <?php endif;?>
</div>
<div id="main_content" class="ui-widget ui-widget-content ui-corner-all">
    <div class="form">
        <?php echo jq_form_remote_tag(
                array(
                    'update' => 'main',
                    'url' => ($form->getObject()->isNew() ?
                                url_for('element_create').'?type_id='.$type->getId().'&section_id='.$section->getId()
                                :
                                url_for('element_update',$form->getObject())
                                ),
                    'before'   => 'hideMainContent();showLoaderMain();',
                    'complete' => 'hideLoader();showMainContent();',
                    'script'   => true
                ),array(
                    'id' => 'file_upload',
                    'class' => 'form_menu'
                ))?>
        <?php if (!$form->getObject()->isNew()): ?>
            <input type="hidden" name="sf_method" value="put" />
        <?php endif; ?>
            <fieldset>
                <?php echo $form->renderHiddenFields() ?>
                <?php echo $form->renderGlobalErrors() ?>
                <div class="form-row">
                    <?php echo $form['title']->renderError() ?>
                    <?php echo $form['title']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['title']->render() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['Menu']['render_title']->renderError() ?>
                    <?php echo $form['Menu']['render_title']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['Menu']['render_title']->render() ?>
                    </div>
                </div>
                <div class="form-row">
                    <?php echo $form['Menu']['is_vertical']->renderError() ?>
                    <?php echo $form['Menu']['is_vertical']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['Menu']['is_vertical']->render() ?>
                    </div>
                </div>
                <div id="menu_uses_bullet" class="form-row"
                     style="display:<?php echo ($form->getObject()->getMenu()->getIsVertical() ? 'block' : 'none')?>">
                    <?php echo $form['Menu']['uses_bullet']->renderError() ?>
                    <?php echo $form['Menu']['uses_bullet']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['Menu']['uses_bullet']->render() ?>
                    </div>
                </div>
                <div id="menu_bullet" class="form-row" style="display:
                    <?php echo ($form->getObject()->getMenu()->getIsVertical() && $form->getObject()->getMenu()->getUsesBullet()
                            ? 'block' : 'none')?>">
                    <?php echo $form['Menu']['bullet']->renderError() ?>
                    <?php echo $form['Menu']['bullet_type']->renderError() ?>
                    <?php echo $form['Menu']['bullet_size']->renderError() ?>
                    <?php echo $form['Menu']['tmp_bullet']->renderError() ?>
                    <table id="<?php echo 'files'?>" class="upload-files-table">
                        <?php if ($form['Menu']['bullet']->getValue()): ?>
                            <tr>
                                <td>Archivo actual:</td>
                                <td class="image-preview">
                                    <img alt="<?php echo $form['Menu']['bullet']->getValue()?>"
                                         src="<?php echo '/uploads/portal_'.
                                            ($form->getObject()->getSectionId() ?
                                                $form->getObject()->getSection()->getPortalId()
                                                :
                                                ($form->getObject()->getHeaderId() ?
                                                    $form->getObject()->getHeader()->getPortalId()
                                                    :
                                                    $form->getObject()->getFooter()->getPortalId()
                                                )
                                            ).
                                            ($form->getEmbeddedForm('Menu')->getObject()->getBullet() ?
                                                (!$form->isBound() ?
                                                    '/images/'
                                                    :
                                                    ($form['Menu']['bullet']->getValue() == $form->getEmbeddedForm('Menu')
                                                                            ->getObject()->getBullet()
                                                        ?
                                                        '/images/'
                                                        :
                                                        '/temp_files/'
                                                    )
                                                )
                                                :
                                                '/temp_files/'
                                            ).
                                            basename($form['Menu']['bullet']->getValue())
                                    ?>">
                                </td>
                                <td class="bullet-size">
                                    Tamaño del archivo: <?php echo round(($form['Menu']['bullet_size']->getValue()/1024),2) ?> Kb
                                </td>
                            </tr>
                        <?php endif;?>
                    </table>
                    <?php echo $form['Menu']['tmp_bullet']->renderLabel() ?>
                    <div id="<?php echo 'drop_zone'?>" class="form-field drop-zone">
                        <?php echo $form['Menu']['tmp_bullet']->render() ?>
                        <div>Archivo a cargar</div>
                    </div>
                    <?php echo $form['Menu']['tmp_bullet']->renderHelp() ?>
                </div>
                <?php if($form->hasGlobalErrors()): ?>
                    <?php echo $form['Menu']['Options']->renderError() ?>
                <?php endif; ?>
                <?php foreach ($form['Menu']['Options'] as $i => $option): ?>
                    <fieldset id="<?php echo sprintf('option_%d', $i)?>"  <?php echo $i == 0 ? 'class="first"' : ''?>>
                        <legend>Opción</legend>
                        <div class="form-row">
                            <?php echo $option['is_local']->renderError() ?>
                            <?php echo $option['is_local']->renderLabel() ?>
                            <div class="form-field">
                                <?php echo $option['is_local']->render() ?>
                            </div>
                        </div>
                        <div id="<?php echo 'menu_option_local_'.$i ?>"
                             style="display:
                                <?php echo ($option['is_local']->getValue() ? 'block' : 'none')?>"
                             >
                            <div class="form-row">
                                <?php echo $option['section_id']->renderError() ?>
                                <?php echo $option['section_id']->renderLabel() ?>
                                <div class="form-field">
                                    <?php echo $option['section_id']->render() ?>
                                </div>
                            </div>
                        </div>
                        <?php echo jq_javascript_tag()?>
                            $('input#ems_element_Menu_Options_<?php echo $i ?>_is_local_1').change(
                                function(){
                                    if($(this).is(':checked')){
                                        $('div#menu_option_local_<?php echo $i ?>').fadeIn("slow");
                                        $('div#menu_option_external_<?php echo $i ?>').fadeOut("slow");
                                        $('div#menu_option_external_<?php echo $i ?>').children('.form-row').children('div.error_list').remove();
                                        $('input#ems_element_Menu_Options_<?php echo $i ?>_title').val('');
                                        $('input#ems_element_Menu_Options_<?php echo $i ?>_link').val('');
                                    }
                                }
                            );
                            $('input#ems_element_Menu_Options_<?php echo $i ?>_is_local_0').change(
                                function(){
                                    if($(this).is(':checked')){
                                        $('div#menu_option_local_<?php echo $i ?>').fadeOut("slow");
                                        $('div#menu_option_external_<?php echo $i ?>').fadeIn("slow");
                                    }
                                }
                            );
                        <?php echo jq_end_javascript_tag()?>
                        <div id="<?php echo 'menu_option_external_'.$i ?>"
                             style="display:
                                <?php echo ($option['is_local']->getValue() ? 'none' : 'block')?>"
                             >
                            <div class="form-row">
                                <?php echo $option['title']->renderError() ?>
                                <?php echo $option['title']->renderLabel() ?>
                                <div class="form-field">
                                    <?php echo $option['title']->render() ?>
                                </div>
                            </div>
                            <div class="form-row">
                                <?php echo $option['link']->renderError() ?>
                                <?php echo $option['link']->renderLabel() ?>
                                <div class="form-field">
                                    <?php echo $option['link']->render() ?>
                                    <?php echo $option['link']->renderHelp() ?>
                                </div>
                            </div>
                        </div>
                        <?php if(isset ($option['is_enabled'])):?>
                            <div class="form-row">
                                <?php echo $option['is_enabled']->renderError() ?>
                                <?php echo $option['is_enabled']->renderLabel() ?>
                                <div class="form-field">
                                    <?php echo $option['is_enabled']->render() ?>
                                </div>
                            </div>
                        <?php else: ?>
                            <?php if($i>0):?>
                                <a class="remove" href="#">Eliminar</a>
                            <?php endif;?>
                        <?php endif;?>
                    </fieldset>
                <?php endforeach; ?>
                <div id="extraoptions"></div>
                <div class="add-button">
                    <a class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"
                            id="add_option" href="#" aria-disabled="false">
                        <span class="ui-button-icon-primary ui-icon ui-icon-plusthick"></span>
                        <span class="ui-button-text">Agregar</span>
                    </a>
                </div>
                <div class="form-row">
                    <?php echo $form['position']->renderError() ?>
                    <?php echo $form['position']->renderLabel() ?>
                    <div class="form-field">
                        <?php echo $form['position']->render() ?>
                    </div>
                </div>
                <?php if(isset ($form['published'])):?>
                    <div class="form-row">
                        <?php echo $form['published']->renderError() ?>
                        <?php echo $form['published']->renderLabel() ?>
                        <div class="form-field">
                            <?php echo $form['published']->render() ?>
                        </div>
                    </div>
                <?php endif;?>
                <?php if(isset ($form['is_enabled'])):?>
                    <div class="form-row">
                        <?php echo $form['is_enabled']->renderError() ?>
                        <?php echo $form['is_enabled']->renderLabel() ?>
                        <div class="form-field">
                            <?php echo $form['is_enabled']->render() ?>
                        </div>
                    </div>
                <?php endif;?>
            </fieldset>
            <div id="form_buttons" class="ui-widget">
                <input id="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-button-text"
                           type="submit" value="Guardar" title="Guardar"/>
                <?php if($form->getObject()->isNew()):?>
                    <?php echo jq_link_to_remote(
                        '<span class="ui-button-text">Cancelar</span>',
                        array(
                            'update' => 'main',
                            'url' => url_for('section_show', $form->getObject()->getSection()),
                            'before' => 'clearView();showLoaderMain();',
                            'complete' => 'hideLoader();',
                            'script' => true
                        ), array(
                            'class' => 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only',
                            'title' => 'Cancelar'
                        )) ?>
                <?php else: ?>
                    <?php echo jq_link_to_remote(
                            '<span class="ui-button-text">Cancelar</span>',
                        array(
                            'update' => 'main',
                            'url' => url_for('element_show',$form->getObject()),
                            'before' => 'clearView();showLoaderMain();',
                            'complete' => 'hideLoader();',
                            'script' => true
                        ), array(
                            'class' => 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only',
                            'title' => 'Cancelar'
                        )) ?>
                <?php endif; ?>
            </div>
        </form>
    </div>
</div>